package com.woniuxy.hrp.service.Serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.hrp.entity.Department;
import com.woniuxy.hrp.entity.Doctor;
import com.woniuxy.hrp.entity.Hospital;
import com.woniuxy.hrp.entity.PageBean;
import com.woniuxy.hrp.mapper.DepartmentMapper;
import com.woniuxy.hrp.mapper.DoctorMapper;
import com.woniuxy.hrp.mapper.HospitalMapper;
import com.woniuxy.hrp.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: hrph
 * @ClassName HospitalServiceImpl
 * @description:
 * @author: LRW
 * @create: 2022-08-30 17:36
 * @Version 1.0
 **/

@Service
@Transactional
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalMapper hospitalMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    //增加医院
    @Override
    public void add(Hospital hospital) {
        hospital.setStatus("y");
        hospitalMapper.add(hospital);
    }

    @Override
    public List<Hospital> seleAll() {
       return hospitalMapper.selectAll();
    }

    //删除医院及其包含医生(逻辑删除)
    @Override
    public void delete(int id) {
        //根据医院id查询出所拥有的科室
        List<Department> departments = departmentMapper.findByHospitalid(id);
        //根据科室id查询出所属的医生
        for (Department department : departments) {
            Doctor doctor = doctorMapper.selectByDepartmentid(department.getId());
            //删除医生(逻辑删除)
            if(doctor!=null){
                doctorMapper.delete(doctor.getId());
            }

        }

        //删除医院（逻辑删除）
        hospitalMapper.update(id);
    }

    //修改医院信息
    @Override
    public void updateByCondition(Hospital hospital) {
        hospitalMapper.updateByCondition(hospital);
    }

    //查询医院（根据id）
    @Override
    public Hospital findById(int id) {
        Hospital hospital = hospitalMapper.findById(id);
        return hospital;
    }

    //查询所有医院
    @Override
    public PageBean<Hospital> findAll(int page) {
        PageBean<Hospital> pageBean = new PageBean<Hospital>();
        int pageSize = 3;//首页显示每页条数
        Page<Object> p = PageHelper.startPage(page, pageSize);
        List<Hospital> hospitals = hospitalMapper.all();
        pageBean.setData(hospitals);
        pageBean.setCurrPage(page);
        pageBean.setCurrPageSize(pageSize);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    //根据名字模糊查询医院
    @Override
    public PageBean<Hospital> findByName(int page, String name) {
        PageBean<Hospital> pageBean = new PageBean<Hospital>();
        int pageSize = 3;//首页显示每页条数
        Page<Object> p = PageHelper.startPage(page, pageSize);
        List<Hospital> hospitals = hospitalMapper.findByName(name);
        pageBean.setData(hospitals);
        pageBean.setCurrPage(page);
        pageBean.setCurrPageSize(pageSize);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    /***
     * @description: 系统内所有医院列表展示
     * @param:
     * @return: java.util.List<com.woniuxy.hrp.entity.Hospital>
     * @author Lrw
     * @date: 2022/8/30 17:37
     */
    @Override
    public List<Hospital> getAll() {
        List<Hospital> hospitals = hospitalMapper.all();
        return hospitals;
    }

    /***
     * @description: 多条件查询医院
     * @param: hospital
     * @return: java.util.List<com.woniuxy.hrp.entity.Hospital>
     * @author Lrw
     * @date: 2022/8/30 18:32
     */
    @Override
    public List<Hospital> getByCondition(Hospital hospital) {
        List<Hospital> hospitals = hospitalMapper.selectByCondition(hospital);
        return hospitals;
    }

    /***
     * @description: 根据id查医院详细信息（医院详情页）
     * @param: id
     * @return: com.woniuxy.hrp.entity.Hospital
     * @author Lrw
     * @date: 2022/8/30 18:36
     */
    @Override
    public Hospital getById(int id) {
        Hospital hospital = hospitalMapper.selectByIdWithDept(id);
        return hospital;
    }

    /***
     * @description: 分页查询
     * @param: page
     * @return: com.woniuxy.hrp.entity.PageBean<com.woniuxy.hrp.entity.Hospital>
     * @author Lrw
     * @date: 2022/9/2 14:32
     */
    @Override
    public PageBean<Hospital> getByPage(int page) {
        PageBean<Hospital> pageBean = new PageBean<Hospital>();
        int pageSize = 4;//首页显示每页条数
        Page<Object> p = PageHelper.startPage(page, pageSize);
        List<Hospital> hospitals = hospitalMapper.selectAllHospAvailable();
        pageBean.setData(hospitals);
        pageBean.setCurrPage(page);
        pageBean.setCurrPageSize(pageSize);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    /***
     * @description: 根据名字分页模糊查询
     * @param: page
     * @return: com.woniuxy.hrp.entity.PageBean<com.woniuxy.hrp.entity.Hospital>
     * @author Lrw
     * @date: 2022/9/2 16:24
     */
    @Override
    public PageBean<Hospital> getByNameAndPageVaguely(int page, String name) {
        PageBean<Hospital> pageBean = new PageBean<Hospital>();
        int pageSize = 4;//首页显示每页条数
        Page<Object> p = PageHelper.startPage(page, pageSize);
        List<Hospital> hospitals = hospitalMapper.selectByHospNameVaguely(name);
        pageBean.setData(hospitals);
        pageBean.setCurrPage(page);
        pageBean.setCurrPageSize(pageSize);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    /***
     * @description: 根据医院名模糊查找（首页查询弹出框）
     * @param: name
     * @return: com.woniuxy.hrp.entity.Hospital
     * @author Lrw
     * @date: 2022/9/3 11:55
     */
    @Override
    public List<Hospital> getByNameVag(String name) {
        return hospitalMapper.selectByHospNameVaguely(name);
    }


}
